﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public void MapFBST(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveFBST(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryFBST(dwData, 3);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapFBST(ref dwLocal, dwData);

            if (!flgExist)
            {
                InsertFBST(dwLocal);
            }
            UpdateFBST(dwLocal);
        }
        public void InsertFBST(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "INSERT INTO FBST(FBMANDANT, FBART, FBVAR, FBFARBE, FBGROE, FBAUFM, FBVPEINH, FBANZVP, FBZS, FBCHAR, FBGNR, FBGNRPOS, FBLO, FBLB, FBLP, FBME, FBZUME, FBAUFBKZ, FBCHARL, FBCHARLZUS, FBSPKZ, FBSPSTA, FBSPGRD, FBFILLER, FBAENDZL)"
            + " VALUES (:FBMANDANT, :FBART, :FBVAR, :FBFARBE, :FBGROE, '  ', '   ', '0000', '1A',:FBCHAR, '0000', '0000', :FBLO, :FBLB, :FBLP, 1, 0, 'N', '               ', '               ', ' ', '  ', '  ', '                                                                           ', 6)";
            or1.Parameters.Add("FBMANDANT", OracleType.NVarChar).Value = dwData["FBMANDANT"].ToString();
            or1.Parameters.Add("FBART", OracleType.NVarChar).Value = dwData["FBART"].ToString();
            or1.Parameters.Add("FBVAR", OracleType.NVarChar).Value = dwData["FBVAR"].ToString();
            or1.Parameters.Add("FBFARBE", OracleType.NVarChar).Value = dwData["FBFARBE"].ToString();
            or1.Parameters.Add("FBGROE", OracleType.NVarChar).Value = dwData["FBGROE"].ToString();
            or1.Parameters.Add("FBCHAR", OracleType.NVarChar).Value = dwData["FBCHAR"].ToString();
            or1.Parameters.Add("FBLO", OracleType.NVarChar).Value = dwData["FBLO"].ToString();
            or1.Parameters.Add("FBLB", OracleType.NVarChar).Value = dwData["FBLB"].ToString();
            or1.Parameters.Add("FBLP", OracleType.NVarChar).Value = dwData["FBLP"].ToString();
            Connect2Proxy(or1);
        }
        public void DeleteFBST(DataRow dwData, int option = 1)
        {
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM FBST "
                       + " WHERE FBMANDANT=:FBMANDANT AND FBCHAR=:FBCHAR AND FBLO=:FBLO AND FBLB=:FBLB AND FBLP=:FBLP ";
                    or1.Parameters.Add("FBMANDANT", OracleType.NVarChar).Value = dwData["FBMANDANT"];
                    or1.Parameters.Add("FBCHAR", OracleType.NVarChar).Value = dwData["FBCHAR"];
                    or1.Parameters.Add("FBLO", OracleType.NVarChar).Value = dwData["FBLO"];
                    or1.Parameters.Add("FBLB", OracleType.NVarChar).Value = dwData["FBLB"];
                    or1.Parameters.Add("FBLP", OracleType.NVarChar).Value = dwData["FBLP"];
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        }
        public void UpdateFBST(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "UPDATE FBST SET "
                     + "FBMANDANT=:FBMANDANT,"
                     + "FBART=:FBART,"
                     + "FBVAR=:FBVAR,"
                     + "FBFARBE=:FBFARBE,"
                     + "FBGROE=:FBGROE,"
                     + "FBAUFM=:FBAUFM,"
                     + "FBVPEINH=:FBVPEINH,"
                     + "FBANZVP=:FBANZVP,"
                     + "FBZS=:FBZS,"
                     + "FBCHAR=:FBCHAR,"
                     + "FBGNR=:FBGNR,"
                     + "FBGNRPOS=:FBGNRPOS,"
                     + "FBLO=:FBLO,"
                     + "FBLB=:FBLB,"
                     + "FBLP=:FBLP,"
                     + "FBME=:FBME,"
                     + "FBZUME=:FBZUME,"
                     + "FBAUFBKZ=:FBAUFBKZ,"
                     + "FBCHARL=:FBCHARL,"
                     + "FBCHARLZUS=:FBCHARLZUS,"
                     + "FBSPKZ=:FBSPKZ,"
                     + "FBSPSTA=:FBSPSTA,"
                     + "FBSPGRD=:FBSPGRD,"
                     + "FBFILLER=:FBFILLER,"
                     + "FBAENDZL=:FBAENDZL"
                     + " WHERE  "
                     + "  FBMANDANT=:FBMANDANT AND FBCHAR=:FBCHAR AND FBLO=:FBLO AND FBLB=:FBLB AND FBLP=:FBLP ";
                    or1.Parameters.Add("FBMANDANT", OracleType.NVarChar).Value = dwData["FBMANDANT"].ToString();
                    or1.Parameters.Add("FBART", OracleType.NVarChar).Value = dwData["FBART"].ToString();
                    or1.Parameters.Add("FBVAR", OracleType.NVarChar).Value = dwData["FBVAR"].ToString();
                    or1.Parameters.Add("FBFARBE", OracleType.NVarChar).Value = dwData["FBFARBE"].ToString();
                    or1.Parameters.Add("FBGROE", OracleType.NVarChar).Value = dwData["FBGROE"].ToString();
                    or1.Parameters.Add("FBAUFM", OracleType.NVarChar).Value = dwData["FBAUFM"].ToString();
                    or1.Parameters.Add("FBVPEINH", OracleType.NVarChar).Value = dwData["FBVPEINH"].ToString();
                    or1.Parameters.Add("FBANZVP", OracleType.NVarChar).Value = dwData["FBANZVP"].ToString();
                    or1.Parameters.Add("FBZS", OracleType.NVarChar).Value = dwData["FBZS"].ToString();
                    or1.Parameters.Add("FBCHAR", OracleType.NVarChar).Value = dwData["FBCHAR"].ToString();
                    or1.Parameters.Add("FBGNR", OracleType.NVarChar).Value = dwData["FBGNR"].ToString();
                    or1.Parameters.Add("FBGNRPOS", OracleType.NVarChar).Value = dwData["FBGNRPOS"].ToString();
                    or1.Parameters.Add("FBLO", OracleType.NVarChar).Value = dwData["FBLO"].ToString();
                    or1.Parameters.Add("FBLB", OracleType.NVarChar).Value = dwData["FBLB"].ToString();
                    or1.Parameters.Add("FBLP", OracleType.NVarChar).Value = dwData["FBLP"].ToString();
                    or1.Parameters.Add("FBME", OracleType.NVarChar).Value = dwData["FBME"].ToString();
                    or1.Parameters.Add("FBZUME", OracleType.NVarChar).Value = dwData["FBZUME"].ToString();
                    or1.Parameters.Add("FBAUFBKZ", OracleType.NVarChar).Value = dwData["FBAUFBKZ"].ToString();
                    or1.Parameters.Add("FBCHARL", OracleType.NVarChar).Value = dwData["FBCHARL"].ToString();
                    or1.Parameters.Add("FBCHARLZUS", OracleType.NVarChar).Value = dwData["FBCHARLZUS"].ToString();
                    or1.Parameters.Add("FBSPKZ", OracleType.NVarChar).Value = dwData["FBSPKZ"].ToString();
                    or1.Parameters.Add("FBSPSTA", OracleType.NVarChar).Value = dwData["FBSPSTA"].ToString();
                    or1.Parameters.Add("FBSPGRD", OracleType.NVarChar).Value = dwData["FBSPGRD"].ToString();
                    or1.Parameters.Add("FBFILLER", OracleType.NVarChar).Value = dwData["FBFILLER"].ToString();
                    or1.Parameters.Add("FBAENDZL", OracleType.NVarChar).Value = dwData["FBAENDZL"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        }
        public DataSet QueryFBST(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM FBST WHERE  ROWNUM<2 ";
                    break;
                case 1:
                    or1.CommandText = "SELECT * FROM FBST "
                        + " WHERE FBMANDANT=:FBMANDANT AND FBART=:FBART AND FBVAR=:FBVAR AND FBFARBE=:FBFARBE AND FBGROE=:FBGROE AND FBLO=:FBLO AND FBLB=:FBLB AND FBLP=:FBLP";
                    or1.Parameters.Add("FBMANDANT", OracleType.NVarChar).Value = dwData["FBMANDANT"].ToString();
                    or1.Parameters.Add("FBART", OracleType.NVarChar).Value = dwData["FBART"].ToString();
                    or1.Parameters.Add("FBVAR", OracleType.NVarChar).Value = dwData["FBVAR"].ToString();
                    or1.Parameters.Add("FBFARBE", OracleType.NVarChar).Value = dwData["FBFARBE"].ToString();
                    or1.Parameters.Add("FBGROE", OracleType.NVarChar).Value = dwData["FBGROE"].ToString();
                    or1.Parameters.Add("FBLO", OracleType.NVarChar).Value = dwData["FBLO"].ToString();
                    or1.Parameters.Add("FBLB", OracleType.NVarChar).Value = dwData["FBLB"].ToString();
                    or1.Parameters.Add("FBLP", OracleType.NVarChar).Value = dwData["FBLP"].ToString();
                    break;
                case 2:
                    or1.CommandText = "SELECT  1 AS BAID,FBMANDANT AS MANDANT "
                        + "  ,FBART AS ART,FBVAR AS VAR,FBFARBE AS COLOR, FBGROE AS ASIZE,"
                        + "  FBLO AS LO,FBLB AS LB,FBLP AS LP,FBVPEINH AS LOT,FBME AS QTY,0 AS QTYLOCKED,FBME AS QTYFREE"
                        + " FROM FBST "
                        + " WHERE FBMANDANT=:FBMANDANT AND FBART=:FBART AND FBVAR=:FBVAR AND FBFARBE=:FBFARBE AND FBGROE=:FBGROE";
                    or1.Parameters.Add("FBMANDANT", OracleType.NVarChar).Value = dwData["FBMANDANT"].ToString();
                    or1.Parameters.Add("FBART", OracleType.NVarChar).Value = dwData["FBART"].ToString();
                    or1.Parameters.Add("FBVAR", OracleType.NVarChar).Value = dwData["FBVAR"].ToString();
                    or1.Parameters.Add("FBFARBE", OracleType.NVarChar).Value = dwData["FBFARBE"].ToString();
                    or1.Parameters.Add("FBGROE", OracleType.NVarChar).Value = dwData["FBGROE"].ToString();
                    break;
                case 3:
                    or1.CommandText = "SELECT * FROM FBST "
                        + " WHERE FBMANDANT=:FBMANDANT AND FBCHAR=:FBCHAR AND FBLO=:FBLO AND FBLB=:FBLB AND FBLP=:FBLP";
                    or1.Parameters.Add("FBMANDANT", OracleType.NVarChar).Value = dwData["FBMANDANT"].ToString();
                    or1.Parameters.Add("FBCHAR", OracleType.NVarChar).Value = dwData["FBCHAR"].ToString();
                    or1.Parameters.Add("FBLO", OracleType.NVarChar).Value = dwData["FBLO"].ToString();
                    or1.Parameters.Add("FBLB", OracleType.NVarChar).Value = dwData["FBLB"].ToString();
                    or1.Parameters.Add("FBLP", OracleType.NVarChar).Value = dwData["FBLP"].ToString();
                    break;
                case 4:
                    or1.CommandText = "SELECT * FROM FBST "
                        + " WHERE FBMANDANT=:FBMANDANT AND FBART=:FBART AND FBVAR=:FBVAR AND FBFARBE=:FBFARBE AND FBGROE=:FBGROE AND FBLO=:FBLO AND FBLB=:FBLB"
                        + " ORDER BY FBCHAR";
                    or1.Parameters.Add("FBMANDANT", OracleType.NVarChar).Value = dwData["FBMANDANT"].ToString();
                    or1.Parameters.Add("FBART", OracleType.NVarChar).Value = dwData["FBART"].ToString();
                    or1.Parameters.Add("FBVAR", OracleType.NVarChar).Value = dwData["FBVAR"].ToString();
                    or1.Parameters.Add("FBFARBE", OracleType.NVarChar).Value = dwData["FBFARBE"].ToString();
                    or1.Parameters.Add("FBGROE", OracleType.NVarChar).Value = dwData["FBGROE"].ToString();
                    or1.Parameters.Add("FBLO", OracleType.NVarChar).Value = dwData["FBLO"].ToString();
                    or1.Parameters.Add("FBLB", OracleType.NVarChar).Value = dwData["FBLB"].ToString();
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "FBST";
            Return = Result;
            return Return;
        }
    }
}
